package com.xuecheng.content.service.Impl;

import com.xuecheng.content.mapper.CourseTeacherMapper;
import com.xuecheng.content.model.po.CourseTeacher;
import com.xuecheng.content.service.CourseTeacherService;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.List;

@Service
public class CourseTeacherServiceImpl implements CourseTeacherService {
    @Autowired
    private CourseTeacherMapper courseTeacherMapper;
    @Override
    public List<CourseTeacher> getTeachers(Long company, Long courseId) {
        List<CourseTeacher> teachers = courseTeacherMapper.selectByCourseId(courseId);
        return teachers;
    }

    @Override
    public CourseTeacher saveAndUpdate(CourseTeacher courseTeacher) {
        //如果id为空说明是添加教师，否则是修改教师
        CourseTeacher newCourseTeacher;
        if(courseTeacher.getId() == null) {
            newCourseTeacher = new CourseTeacher();
            courseTeacherMapper.insert(courseTeacher);
            Long id = courseTeacher.getId();
            BeanUtils.copyProperties(courseTeacher, newCourseTeacher);
            newCourseTeacher.setId(id);
        }else{
            newCourseTeacher = courseTeacherMapper.selectById(courseTeacher.getId());
            BeanUtils.copyProperties(courseTeacher,newCourseTeacher);
            courseTeacherMapper.updateById(newCourseTeacher);
        }
        return newCourseTeacher;
    }

    @Override
    public void deleteTeacher(Long company, Long courseId, Long id) {
        courseTeacherMapper.deleteById(id);
    }
}
